using System.Collections;
using System.Collections.Generic;
using Main.Details;
using Main.Inventory;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using ZSW.Framework;
using ZSW.Framework.UIElement;
using ZSW.Framework.Utils;

namespace Main.UI
{
    public class UI_ToolTip_Item : ZSWF_BaseUIElement
    {
        [SerializeField] private Vector2 offset;
        [SerializeField] private RectTransform rectTransform;
        [SerializeField] private TextMeshProUGUI nameText;
        [SerializeField] private TextMeshProUGUI typeText;
        [SerializeField] private TextMeshProUGUI descriptionText;
        [SerializeField] private Text valueText;
        [SerializeField] private GameObject bottomPart;

        [Header("蓝图需求")]
        [SerializeField] private TextMeshProUGUI titleText;
        [SerializeField] private Image[] resourceImages;
        [SerializeField] private Text[] resourceTexts;
        public override void Init()
        {
            base.Init();
        }

        public void SetResourceItem(int bluePrintId)
        {
            if (!ZSWF_IOCContainer.Get<BluePrintDataList_SO>().TryGetBluePrintDetails(bluePrintId, out var bluePrintDetails))
            {
                titleText.SetActive(false);
                foreach (var resourceImage in resourceImages)
                {
                    resourceImage.SetActive(false);
                }
                return;
            }
            else
            {
                titleText.SetActive(true);
            }

            var itemDatabase = ZSWF_IOCContainer.Get<InventoryManager>();
            for (int i = 0; i < bluePrintDetails.resourceItem.Length; i++)
            {
                if (i < resourceImages.Length)
                {
                    var item = bluePrintDetails.resourceItem[i];
                    if (itemDatabase.TryGetItemDetails(item.ItemID, out var itemDetails))
                    {
                        resourceImages[i].gameObject.SetActive(item.ItemID != 0);
                        resourceImages[i].sprite = itemDetails.ItemIcon;
                    }
                    else
                    {
                        resourceImages[i].gameObject.SetActive(false);
                        resourceTexts[i].text = item.ItemAmount.ToString();
                    }
                }
            }
        }

        public void SetupToolTip(ItemDetails itemDetails, SlotType slotType, Vector2 position)
        {
            rectTransform.position = position + offset;

            nameText.text = itemDetails.ItemName;
            typeText.text = itemDetails.ItemType.ToCN();
            descriptionText.text = itemDetails.ItemDescription;

            if (itemDetails.ItemType == ItemType.Seed ||
                itemDetails.ItemType == ItemType.Commodity ||
                itemDetails.ItemType == ItemType.Furniture)
            {
                bottomPart.SetActive(true);

                int price = itemDetails.ItemPrice;
                if (slotType == SlotType.Bag)
                {
                    price = (int)(price * itemDetails.SellPercentage);
                }

                valueText.text = price.ToString();
            }
            else
            {
                bottomPart.SetActive(false);
            }

            LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);
        }
    }
}

